/*
 * Copyright 2007 Jeffrey Palm
 */

public class Util {
  
  public static String getAttribute(String attr, String line) {
    if (line == null) return line;
    String target = attr + "=\"";
    int itarget = line.indexOf(target);
    if (itarget == -1) return null;
    int iq = line.indexOf("\"",itarget+1+target.length());
    if (iq == -1) return null;
    return line.substring(itarget+target.length(),iq);
  }

  public static String getBetweenTags(String line) {
    int ia = line.indexOf(">");
    if (ia == -1) return null;
    int ib = line.indexOf("<",ia+1);
    if (ib == -1) return null;
    return line.substring(ia+1,ib);
  }

  public static String removeTags(String s) {
    if (s == null) return s;
    boolean inTags = false;
    StringBuffer res = new StringBuffer();
    for (int i=0; i<s.length(); i++) {
      char c = s.charAt(i);
      if (c == '<') {
	inTags = true;
      } else if (c == '>') {
	inTags = false;
      } else if (!inTags) {
	res.append(c);
      }
    }
    return res.toString();
  }
}
